package algorithm.leetCode;

public class Z {

	public static void main(String[] args) {
		String str = "LEETCODEISHIRING";
		System.out.println(convert(str, 3));
		System.out.println(convert(str, 4));
	}

	public static String convert(String s, int numRows) {
		if (numRows == 1)
			return s;
		int length = s.length();
		char[] cs = s.toCharArray();
		int[] arr = new int[length];
		int x = 0, g = (2 * numRows - 2);
		for (int i = 0; i < length; i++) {
			arr[i] = x < numRows ? x : g - x;
			x++;
			if (x % g == 0)
				x = 0;
		}
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < numRows; i++)
			for (int j = 0; j < length; j++)
				if (arr[j] == i)
					str.append(cs[j]);
		return str.toString();
	}
}
